package com.like.shopmanager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.like.entity.LoginedUser;
import com.like.entity.ReportInfo;
import com.like.likeutils.network.RetrofitCallback;
import com.like.likeutils.network.RetrofitUtil;
import com.like.network.AmountService;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.activity_chart)
public class ChartActivity extends BaseActivity {

    @ViewInject(R.id.inmoney)
    private TextView mInMoney;
    @ViewInject(R.id.outmoney)
    private TextView mOutMoney;

    private AmountService mAmountService;
    private LoginedUser mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAmountService = RetrofitUtil.getGsonService(AmountService.class);
        mUser = m.getLoginedUser();
        if (mUser == null) {
            Toast.makeText(m.mContext, "请先登陆", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(m.mContext, LoginActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        getReport();
    }

    @Event(R.id.back)
    private void backClick(View v) {
        finish();
    }

    private void getReport() {
        Call<ReportInfo> call = mAmountService.getReport(mUser.storeid);
        call.enqueue(new RetrofitCallback<ReportInfo>() {
            @Override
            public void onResponse(Call<ReportInfo> call, Response<ReportInfo> response) {
                if (!response.isSuccessful()) {
                    Toast.makeText(m.mContext, "获取数据失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                ReportInfo report = response.body();
                mInMoney.setText("￥" + report.inMoney);
                mOutMoney.setText("￥" + report.outMoney);
            }

            @Override
            public void onFailure(Call<ReportInfo> call, Throwable t) {
                super.onFailure(call, t);
                Toast.makeText(m.mContext, "获取数据失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
